package be.jaisy.ui.httpservlet.extender.internal;

import java.util.HashMap;
import java.util.Map;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.BundleListener;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.http.HttpService;

/**
 * @author Stany Blanvalet
 */
public class ServletExtenderListener implements BundleListener {

	private BundleContext context;

	private Map<HttpService, ServletExtenderRegistry> servletExtenderRegistries;
	
	public ServletExtenderListener() {
		super();
		servletExtenderRegistries = new HashMap<HttpService, ServletExtenderRegistry>();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.osgi.framework.BundleListener#bundleChanged(org.osgi.framework.BundleEvent)
	 */
	public void bundleChanged(BundleEvent event) {
		//Activator.logDebug(this.getClass(),"received bundleEvent ["+event.getType()+"] for ["+event.getBundle().getBundleId()+"].["+ event.getBundle().getSymbolicName()+"].");
		switch (event.getType()) {
		case BundleEvent.STARTED:
			for (HttpService httpService : this.servletExtenderRegistries.keySet()) {
				addServletExtenderRegistry(event.getBundle(), httpService);
			}			
			break;

		case BundleEvent.STOPPED:
			for (ServletExtenderRegistry servletExtenderRegistry : this.servletExtenderRegistries.values()) {
				if(servletExtenderRegistry!=null)
					servletExtenderRegistry.unregister(event.getBundle());
			}
			break;
		}
	}

	protected void activate(ComponentContext cc) {
		//Activator.logDebug(this.getClass(),"activation started.");
		this.context = cc.getBundleContext();
		this.context.addBundleListener(this);
		for (HttpService httpService : this.servletExtenderRegistries.keySet()) {
			for (Bundle bundle : context.getBundles()) {
				if ((bundle.getState() & (Bundle.STARTING | Bundle.ACTIVE)) != 0){
					addServletExtenderRegistry(bundle, httpService);
				}
			}
		}	
	}

	protected void deactivate(ComponentContext context) throws Exception {
		Activator.logDebug(this.getClass(),"deactivation started.");
		this.context.removeBundleListener(this);
		for (HttpService httpService : this.servletExtenderRegistries.keySet()) {
			removeHttpService(httpService);
		}		
	}
	


	public void addHttpService(HttpService httpService) {
		this.servletExtenderRegistries.put(httpService, null);
	}
	
	public void removeHttpService(HttpService httpService) {
		ServletExtenderRegistry servletExtenderRegistry = this.servletExtenderRegistries.remove(httpService);
		if(servletExtenderRegistry!=null)
		{
			servletExtenderRegistry.close();
		}
	}
	
	private void addServletExtenderRegistry(Bundle bundle, HttpService httpService){
		//Activator.logDebug(this.getClass(),"register bundle ["+bundle.getBundleId()+"].["+ bundle.getSymbolicName()+"] with HttpService ["+httpService.toString()+"].");
		ServletExtenderRegistry servletExtenderRegistry = this.servletExtenderRegistries.get(httpService);
		if(servletExtenderRegistry==null)
			servletExtenderRegistry = new ServletExtenderRegistry(httpService);
		servletExtenderRegistry.register(bundle);
		this.servletExtenderRegistries.put(httpService, servletExtenderRegistry);
	}
}
